package com.ruoyi.wzgl.wzrk.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wzgl.wzrk.domain.Wzrk;
import com.ruoyi.wzgl.wzrk.domain.WzrkRkmx;
import com.ruoyi.wzgl.wzrk.mapper.WzrkMapper;
import com.ruoyi.wzgl.wzrk.service.IWzrkService;
import com.ruoyi.wzgl.wzrk.vo.RkZeByMonthVo;
import com.ruoyi.wzgl.wzrk.vo.WzRklVo;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物资入库Service业务层处理
 *
 * @author huacai
 * @date 2024-09-23
 */
@Service
public class WzrkServiceImpl implements IWzrkService {
    @Resource
    private WzrkMapper wzrkMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物资入库
     *
     * @param wzrkId 物资入库主键
     * @return 物资入库
     */
    @Override
    public Wzrk selectWzrkByWzrkId(Integer wzrkId) {
        return wzrkMapper.selectWzrkByWzrkId(wzrkId);
    }

    /**
     * 查询物资入库列表
     *
     * @param wzrk 物资入库
     * @return 物资入库
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Wzrk> selectWzrkList(Wzrk wzrk) {
        return wzrkMapper.selectWzrkList(wzrk);
    }

    /**
     * 新增物资入库
     *
     * @param wzrk 物资入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWzrk(Wzrk wzrk) {
        wzrk.setCreateTime(DateUtils.getNowDate());
        int rows = wzrkMapper.insertWzrk(wzrk);
        insertWzrkRkmx(wzrk);
        return rows;
    }

    /**
     * 批量新增物资入库
     *
     * @param wzrks 物资入库List
     * @return 结果
     */
    @Override
    public int batchInsertWzrk(List<Wzrk> wzrks) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wzrks)) {
            try {
                for (int i = 0; i < wzrks.size(); i++) {
                    int row = wzrkMapper.insertWzrk(wzrks.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == wzrks.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改物资入库
     *
     * @param wzrk 物资入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWzrk(Wzrk wzrk) {
        wzrk.setUpdateTime(DateUtils.getNowDate());
        wzrkMapper.deleteWzrkRkmxByWzrkId(wzrk.getWzrkId());
        insertWzrkRkmx(wzrk);
        return wzrkMapper.updateWzrk(wzrk);
    }

    /**
     * 批量删除物资入库
     *
     * @param wzrkIds 需要删除的物资入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzrkByWzrkIds(Integer[] wzrkIds) {
        wzrkMapper.deleteWzrkRkmxByWzrkIds(wzrkIds);
        return wzrkMapper.deleteWzrkByWzrkIds(wzrkIds);
    }

    /**
     * 删除物资入库信息
     *
     * @param wzrkId 物资入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzrkByWzrkId(Integer wzrkId) {
        wzrkMapper.deleteWzrkRkmxByWzrkId(wzrkId);
        return wzrkMapper.deleteWzrkByWzrkId(wzrkId);
    }

    /**
     * 查询本月入库单量
     * @return
     */
    @Override
    public String getByRkDl() {
        return wzrkMapper.getByRkDl();
    }

    /**
     * 查询本月出库单量
     * @return
     */
    @Override
    public Integer getByCkDl() {
        return wzrkMapper.getByCkDl();
    }

    /**
     * 采购物资数量top10
     * @return
     */
    @Override
    public List<WzRklVo> selectWzRkl() {
        return wzrkMapper.selectWzRkl();
    }

    /**
     * 近6个月每个月的总额
     * @return
     */
    @Override
    public List<RkZeByMonthVo> selectRkZeByMonth() {
        return wzrkMapper.selectRkZeByMonth();
    }

    /**
     * 新增入库明细信息
     *
     * @param wzrk 物资入库对象
     */
    public void insertWzrkRkmx(Wzrk wzrk) {
        List<WzrkRkmx> wzrkRkmxList = wzrk.getWzrkRkmxList();
        Integer wzrkId = wzrk.getWzrkId();
        if (StringUtils.isNotNull(wzrkRkmxList)) {
            List<WzrkRkmx> list = new ArrayList<WzrkRkmx>();
            for (WzrkRkmx wzrkRkmx : wzrkRkmxList) {
                wzrkRkmx.setWzrkId(wzrkId);
                list.add(wzrkRkmx);
            }
            if (list.size() > 0) {
                wzrkMapper.batchWzrkRkmx(list);
            }
        }
    }
}
